package com.example.zhaojianxu.signin.bank.analysis

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bank.http.AnalysisEntity
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.common.http.HttpUtil
import kotlinx.android.synthetic.main.activity_analysis.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AnalysisActivity : BaseActivity() {

    var mAdapter: AnalysisAdapter? = null
    var mList: MutableList<AnalysisEntity> = mutableListOf()

    override fun initView() {
        common_title.text = "查看所有"
        common_setting.visibility = View.GONE
        common_back.setOnClickListener {
            finish()
        }

        mAdapter = AnalysisAdapter(mList)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        activity_analysis_recycler.layoutManager = layoutManager
        activity_analysis_recycler.adapter = mAdapter
    }

    override fun initData() {

        HttpUtil.instance.getBankApi()?.postQueryAnalysis()?.enqueue(object: Callback<MutableList<AnalysisEntity>>{
            override fun onFailure(call: Call<MutableList<AnalysisEntity>>, t: Throwable) {
            }

            override fun onResponse(call: Call<MutableList<AnalysisEntity>>, response: Response<MutableList<AnalysisEntity>>) {
                if (response.isSuccessful && response.body() != null){
                    for (position in 0 until response.body()?.size!!){
                        mList.add(response.body()?.get(position)!!)
                    }
                }
                mAdapter?.notifyDataSetChanged()
            }
        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_analysis)
    }
}
